/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.service;

import java.util.List;
import java.util.UUID;

import com.jeesite.common.datasource.DataSourceHolder;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.own.utils.MessageSaveUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.own.entity.OwnMedicineReceiptInformation;
import com.jeesite.modules.own.dao.OwnMedicineReceiptInformationDao;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 药材收获信息Service
 * @author F
 * @version 2019-09-27
 */
@Service
@Transactional(readOnly=true)
public class OwnMedicineReceiptInformationService extends CrudService<OwnMedicineReceiptInformationDao, OwnMedicineReceiptInformation> {

	@Autowired
	private OwnMedicineReceiptInformationDao ownMedicineReceiptInformationDao;
    @Autowired
	private MessageSaveUtils messageSaveUtils;
	/**
	 * 获取单条数据
	 * @param ownMedicineReceiptInformation
	 * @return
	 */
	@Override
	public OwnMedicineReceiptInformation get(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		return super.get(ownMedicineReceiptInformation);
	}
	
	/**
	 * 查询分页数据
	 * @param ownMedicineReceiptInformation 查询条件
	 * @param ownMedicineReceiptInformation.page 分页对象
	 * @return
	 */
	@Override
	public Page<OwnMedicineReceiptInformation> findPage(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		return super.findPage(ownMedicineReceiptInformation);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param ownMedicineReceiptInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		if(!isEmpty(UserUtils.getUser().getExtend())){
			ownMedicineReceiptInformation.setCreateCompany(UserUtils.getUser().getExtend().getExtendS1());
			String uuid = "";
			//如果是新数据则生成uuid
			if(StringUtils.isBlank(ownMedicineReceiptInformation.getId())){
				uuid = UUID.randomUUID().toString();
				ownMedicineReceiptInformation.setUuid(uuid);
			}else{
				//非新数据，直接获取uuid
				uuid = ownMedicineReceiptInformation.getUuid();
			}
			String txid = messageSaveUtils.getTxid(uuid, "药材收获信息表");
			//System.out.println("txid = " + txid);
			ownMedicineReceiptInformation.setTxid(txid);
		}
		super.save(ownMedicineReceiptInformation);
		DataSourceHolder.setDataSourceName("ds2");
		super.save(ownMedicineReceiptInformation);
		DataSourceHolder.clearDataSourceName();
	}
	
	/**
	 * 更新状态
	 * @param ownMedicineReceiptInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		super.updateStatus(ownMedicineReceiptInformation);
	}
	
	/**
	 * 删除数据
	 * @param ownMedicineReceiptInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		super.delete(ownMedicineReceiptInformation);
	}

	/**
	 * 根据id获取收获信息
	 * @param id
	 * @return
	 */
	public OwnMedicineReceiptInformation findById(String id) {
		return ownMedicineReceiptInformationDao.findById(id);
	}

	public List<OwnMedicineReceiptInformation> findByPlantId(String plantId) {
		return ownMedicineReceiptInformationDao.findByPlantId(plantId);
	}

	/**
	 * 更新检验情况
	 * @param id
	 * @param type
	 */
	@Transactional(readOnly=false)
	public void updateInspectionSituation(String id, String type) {
		ownMedicineReceiptInformationDao.updateInspectionSituation(id, type);
	}
	
}